package cn.minhx.domain.trade.service.settlement.filter;

import cn.minhx.domain.trade.adapter.repostitory.ITradeRepository;
import cn.minhx.domain.trade.moedel.entity.TradeSettlementRuleCommandEntity;
import cn.minhx.domain.trade.moedel.entity.TradeSettlementRuleFilterBackEntity;
import cn.minhx.domain.trade.service.settlement.factor.TradeSettlementRuleFilterFactory;
import cn.minhx.types.design.link.multi.handler.ILogicHandler;
import cn.minhx.types.enums.ResponseCode;
import cn.minhx.types.exception.AppException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * SC 渠道来源过滤 - 当某个签约渠道下架后，则不会记账
 *
 * @author min
 */
@Slf4j
@Service
public class SCRuleFilter
		implements
			ILogicHandler<TradeSettlementRuleCommandEntity, TradeSettlementRuleFilterFactory.DynamicContext, TradeSettlementRuleFilterBackEntity> {

	@Resource
	private ITradeRepository repository;

	@Override
	public TradeSettlementRuleFilterBackEntity apply(TradeSettlementRuleCommandEntity requestParameter,
			TradeSettlementRuleFilterFactory.DynamicContext dynamicContext) throws Exception {
		log.info("结算规则过滤-渠道黑名单校验{} outTradeNo:{}", requestParameter.getUserId(), requestParameter.getOutTradeNo());
		// sc 渠道黑名单拦截
		boolean intercept = repository.isSCBlackIntercept(requestParameter.getSource(), requestParameter.getChannel());
		if (intercept) {
			log.error("{}{} 渠道黑名单拦截", requestParameter.getSource(), requestParameter.getChannel());
			throw new AppException(ResponseCode.E0105);
		}
		return next(requestParameter, dynamicContext);
	}
}
